<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--应用数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDeletes"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getLogList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="日志编号" align="center" key="id" prop="id" v-if="columns[0].visible" />
          <el-table-column label="日志信息" align="center" key="info" prop="info" v-if="columns[1].visible" />
          <el-table-column label="ip信息" align="ip" key="siteName" prop="ip" v-if="columns[2].visible" />
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[3].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"

              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageIndex"
          :limit.sync="queryParams.pageSize"
          @pagination="getLogList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {
  listLog,
  getLog,
  delLog,
  delLogs,
  addLog,
  updateLog,
  getLogByDate,
  exportLog
} from "@/api/system/log";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {convertResBlob} from "@/utils/response";

export default {
  name: "Log",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      logList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        siteName: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `日志编号`, visible: true },
        { key: 1, label: `日志信息`, visible: true },
        { key: 2, label: `ip信息`, visible: true },
        { key: 3, label: `创建时间`, visible: true }
      ],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "应用名称不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "用户密码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getLogList();
  },
  methods: {
    /**根据时间范围搜索*/
    getLogByDate() {
      this.loading = true;
      console.log(this.dateRange);
      getLogByDate(this.addDateRange(this.dateRange)).then(response =>{
        this.siteList = response.data.data;
        this.total = response.data.totalCount;
        this.loading = false;
      })
    },

    /**查询日志列表*/
    getLogList() {
      this.loading = true;
      listLog(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.logList = response.data.data;
          this.total = response.data.totalCount;
          this.loading = false;
        }
      );
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id ;
      this.$confirm('是否确认删除应用编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delLog(id);
      }).then(() => {
        this.getLogList();
        this.msgSuccess("删除成功");
      })
    },
    /** 批量删除按钮操作 */
    handleDeletes() {
      const id = this.ids;
      this.$confirm('是否确认删除日志编号为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delLogs(id);
      }).then(() => {
        this.getLogList();
        this.msgSuccess("删除成功");
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /**搜索*/
    handleQuery() {
      this.getLogList();
    },
    /**导出日志*/
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        exportLog(queryParams).then(res => {
          convertResBlob(res);
        });
      })
    },
  }
};
</script>
